package com.whd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whd.entity.Dept;
import com.whd.entity.Emp;
import com.whd.entity.EmpExample;
import com.whd.mapper.DeptMapper;
import com.whd.mapper.EmpMapper;
import com.whd.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author whd
 * @date 2023/07/01 10:49:55
 */
@Service
@Transactional
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public PageInfo<Emp> findALl(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Emp> list = empMapper.selectByExample(null);
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        for (Emp e : pageInfo.getList()) {
            e.setDept(deptMapper.selectByPrimaryKey(e.getDeptId()));
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Emp> findByOther(Map<String, Object> map) throws ParseException {
        String empName = (String) map.get("empName");
        List<Integer> ids = new ArrayList<>();
        EmpExample empExample =new EmpExample();
        String beginTime = (String) map.get("beginTime");
        String endTime = (String) map.get("endTime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!map.get("deptId").equals("undefined")) {
            Integer deptId = Integer.parseInt((String) map.get("deptId"));
            ids.add(deptId);
            findChildId(deptId, ids);
            empExample.or().andDeptIdIn(ids);
        }
        if (beginTime != null && endTime != null) {
            empExample.or().andEntryDateBetween(sdf.parse(beginTime), sdf.parse(endTime));
        }
         empExample.or().andNameLike("%"+empName+"%");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");

        PageHelper.startPage(pageNum, pageSize);
        List<Emp> list = empMapper.selectByExample(empExample);
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        for (Emp e : pageInfo.getList()) {
            e.setDept(deptMapper.selectByPrimaryKey(e.getDeptId()));
        }
        return pageInfo;
    }

    @Override
    public int save(Emp emp) {
        return empMapper.insert(emp);
    }

    @Override
    public int update(Emp emp) {
        return empMapper.updateByPrimaryKeySelective(emp);
    }

    @Override
    public int delete(Integer id) {
        return empMapper.deleteByPrimaryKey(id);
    }

    //根据id查找部门的子部门
    private void findChildId(Integer deptId,List<Integer> ids) {
        List<Dept> list = deptMapper.selectByExample(null);
        for (Dept dept : list) {
            if (dept.getParentId() == deptId) {
                ids.add(dept.getId());
                findChildId(dept.getId(),ids);
            }
        }
    }
}
